home *** CD-ROM | disk | FTP | other *** search
- /*
- * ODBCdbmsSetup.c -- ODBC Sample DBMS Setup Shared Library (or DLL).
- *
- * (c) Apple Computer, Inc 1993
- */
-
- #include <Dialogs.h>
- #include <Fonts.h>
- #include <LibraryManagerUtilities.h>
- #include <Menus.h>
- #include <QuickDraw.h>
- #include <StdIO.h>
- #include <String.h>
- #include <TextEdit.h>
- #include <Windows.h>
-
- #include "DBMSsetup.h"
- #include "ODBCINST.H"
-
- #include "ODBCdbmsSetup.h"
-
- /*
- * Keywords
- */
-
- LPCSTR kDefaultDataSourceName = "Default"; // default data source name
- LPCSTR kDataSourceNameKeyword = "DSN"; // data source name keyword
- LPCSTR kDescriptionKeyword = "Description"; // data source description keyword
- LPCSTR kOtherKeyword = "Other"; // other keyword
- LPCSTR kTranslateKeyword = "Translate"; // translate keyword
- LPCSTR kTranslateOptionKeyword = "TranlsateOption";// translate option keyword
-
- /*
- * Globals
- */
-
- TLibraryFile *gLibraryFile;
-
- /*
- * prototypes
- */
-
- INSTAPI DoConfigDSN (WindowPtr parentWindow, UINT fRequest, LPCSTR lpszDriver, LPCSTR lpszAttributes);
- INSTAPI RemoveDataSource (WindowPtr parentWindow, LPCSTR lpszDriver, DataSourceInfo *info);
- INSTAPI AddDataSource (WindowPtr parentWindow, LPCSTR lpszDriver, DataSourceInfo *info);
- INSTAPI ConfigureDataSource (WindowPtr parentWindow, LPCSTR lpszDriver, DataSourceInfo *info);
- INSTAPI ParseAttributes (LPCSTR lpszAttributes, DataSourceInfo *info);
- INSTAPI GetKeywordValueFromAttributes (LPCSTR lpszKeyword, LPCSTR lpszAttributes, LPCSTR lpszString);
-
- /*
- * public functions
- */
-
- void
- ODBCdbmsSetupInit()
- {
- /*
- * Shared library init function
- */
-
- GlobalWorld saved;
-
- /*
- * switch to shared library's A5
- */
-
- saved = OpenGlobalWorld();
-
- /*
- * save the local library file so we can quickly preflight later
- */
-
- gLibraryFile = GetLocalLibraryFile();
- if (gLibraryFile == NULL)
- {
- DebugStr("\p GetLocalLibraryFile failed");
- }
-
- /*
- * since we may be posting dialogs the QuickDraw globals, et al. must be initialized
- */
-
- InitGraf(&qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(NULL);
-
- /*
- * always remember to restore original A5
- */
-
- CloseGlobalWorld(saved);
- }
-
- void
- ODBCdbmsSetupCleanUp()
- {
- /*
- * Shared library cleanup function
- */
- }
-
- INSTAPI
- ConfigDSN(GrafPtr pGrafPort, /* window to center over */
- UINT fRequest, /* add, modify or delete */
- LPCSTR lpszDriver, /* name to present to user for this driver */
- LPCSTR lpszAttributes) /* buffer holding section data */
- {
- /*
- * Shared lilbrary entry point for configuring a data source.
- */
-
- INSTAPI success;
- long savedrefnum;
- OSErr err;
- GlobalWorld saved;
-
- /*
- * setup for using shared library globals, resources
- */
-
- saved = OpenGlobalWorld();
- if( err = Preflight( gLibraryFile, &savedrefnum ) )
- {
- DebugStr("\p Preflight failed");
- CloseGlobalWorld(saved);
- return FALSE;
- }
-
- /*
- * here's where the work gets done
- */
-
- success = DoConfigDSN((WindowPtr) pGrafPort, fRequest, lpszDriver, lpszAttributes);
-
- /*
- * restore resource path, A5 world
- */
-
- Postflight(gLibraryFile, savedrefnum);
- CloseGlobalWorld(saved);
-
- return success;
- }
-
- /*
- * private functions
- */
-
- static INSTAPI
- DoConfigDSN(WindowPtr parentWindow, UINT fRequest, LPCSTR lpszDriver, LPCSTR lpszAttributes)
- {
- /*
- * Pull out the keyword values from the attribute buffer, then add, configure or
- * remove the data source. note that the data source name must be one of the
- * keyword values in the buffer (DSN=Data Source Name), or nothing can be done.
- */
-
- INSTAPI success = false;
- DataSourceInfo info;
-
- if (!ParseAttributes(lpszAttributes, &info)) return false;
-
- switch (fRequest)
- {
- case ODBC_ADD_DSN:
- success = AddDataSource(parentWindow, lpszDriver, &info);
- break;
-
- case ODBC_CONFIG_DSN:
- success = ConfigureDataSource(parentWindow, lpszDriver, &info);
- break;
-
- case ODBC_REMOVE_DSN:
- success = RemoveDataSource(parentWindow, lpszDriver, &info);
- break;
- }
-
- return success;
- }
-
- static INSTAPI
- RemoveDataSource(WindowPtr /*parentWindow*/, LPCSTR /*lpszDriver*/, DataSourceInfo *info)
- {
- /*
- * Call the config manager to remove the data source from the ODBC prefs file
- */
-
- return SQLRemoveDSN(info->name);
- }
-
- static INSTAPI
- AddDataSource(WindowPtr parentWindow, LPCSTR lpszDriver, DataSourceInfo *info)
- {
- /*
- * Call the setup dialog on the data source info, then call the config manager to
- * update the data source entry in the ODBC prefs file if the user clicked OK. Make
- * sure to remove the old data source entry as the user may have changed the name.
- */
-
- if (!DoDBMSsetup(parentWindow, info))
- return false;
-
- /*
- * might want to add check that datasource doesn't already exist before writing!?
- */
-
- if (!SQLWriteDSN(info->name, lpszDriver)) return false;
-
- if (!SQLSetKeywordValue(info->name, kDescriptionKeyword,
- info->description, strlen(info->description))) return false;
- if (!SQLSetKeywordValue(info->name, kOtherKeyword,
- info->other, strlen(info->other))) return false;
- if (!SQLSetKeywordValue(info->name, kTranslateKeyword,
- info->translate, strlen(info->translate))) return false;
- if (!SQLSetKeywordValue(info->name, kTranslateOptionKeyword,
- info->translateOption, strlen(info->translateOption))) return false;
-
- return true;
- }
-
- static INSTAPI
- ConfigureDataSource(WindowPtr parentWindow, LPCSTR lpszDriver, DataSourceInfo *info)
- {
- /*
- * Call the setup dialog on the data source info, then call the config manager to
- * update the data source entry in the ODBC prefs file if the user clicked OK. Make
- * sure to remove the old data source entry as the user may have changed the name.
- */
-
- char oldName[kMaxValueLength];
-
- strcpy(oldName, info->name);
-
- if (!DoDBMSsetup(parentWindow, info))
- return false;
-
- if (!SQLRemoveDSN(oldName))
- return false;
-
- if (!SQLWriteDSN(info->name, lpszDriver))
- return false;
-
- if (!SQLSetKeywordValue(info->name, kDescriptionKeyword,
- info->description, strlen(info->description))) return false;
- if (!SQLSetKeywordValue(info->name, kOtherKeyword,
- info->other, strlen(info->other))) return false;
- if (!SQLSetKeywordValue(info->name, kTranslateKeyword,
- info->translate, strlen(info->translate))) return false;
- if (!SQLSetKeywordValue(info->name, kTranslateOptionKeyword,
- info->translateOption, strlen(info->translateOption))) return false;
-
- return true;
- }
-
- static INSTAPI
- ParseAttributes(LPCSTR lpszAttributes, DataSourceInfo *info)
- {
- /*
- * Pull out the keyword values from the data source attributes buffer.
- * If the data source name keyword, DSN, isn't found, or is zero-length, then
- * return false. All other keyword values may be absent or zero-length.
- */
-
- if (!GetKeywordValueFromAttributes(kDataSourceNameKeyword, lpszAttributes, info->name))
- return false;
-
- GetKeywordValueFromAttributes(kDescriptionKeyword, lpszAttributes, info->description);
- GetKeywordValueFromAttributes(kOtherKeyword, lpszAttributes, info->other);
- GetKeywordValueFromAttributes(kTranslateKeyword, lpszAttributes, info->translate);
- GetKeywordValueFromAttributes(kTranslateOptionKeyword, lpszAttributes, info->translateOption);
-
- return true;
- }
-
- static INSTAPI
- GetKeywordValueFromAttributes(LPCSTR lpszKeyword, LPCSTR lpszAttributes, LPCSTR lpszString)
- {
- /*
- * searches the attributes buffer (containing the packed array of keyword=value null-terminated
- * strings, with an additional null terminating the entire array) for the requested keyword,
- * and copies its value over to lpszString. lpszString should point to a buffer at least
- * kMaxValueLength bytes long. returns true if found and non-zero, false otherwise.
- */
-
- char *s, *t, *u;
- char tmpKeyword[kMaxKeywordLength];
-
- /*
- * loop through each keyword=value string in the buffer
- */
-
- for (lpszString[0] = 0, s = lpszAttributes; *s; s += strlen(s) + 1)
- {
- /*
- * pull out the keyword as a c-string
- */
-
- for (u = s, t = tmpKeyword; *u; u++, t++)
- {
- if (*u == '=')
- {
- u++;
- break;
- }
- *t = *u;
- }
- *t = 0;
-
- /*
- * if this is the desired keyword then copy its value to lpszString and beat it. u was
- * left pointing to the start of the value string from the previous operation
- */
-
- if (strcmp(tmpKeyword, lpszKeyword) == 0)
- {
- strcpy(lpszString, u);
- return true;
- }
- }
-
- return false;
- }
-